package org.example.ymcc.consumer;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.example.ymcc.constant.Constants;
import org.example.ymcc.constant.MQConstants;
import org.example.ymcc.constant.RConst;
import org.example.ymcc.domain.CourseOrder;
import org.example.ymcc.mapper.CourseOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

@Slf4j
@Component
@RocketMQMessageListener(
        consumerGroup = "OrderConsumer",
        topic = MQConstants.ALI_PAY_CALLBACK_ORDER_TOPIC,
        selectorExpression = MQConstants.ALI_PAY_CALLBACK_ORDER_TAG
)
public class OrderConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private CourseOrderMapper courseOrderMapper;

    /**
     * 修改订单状态
     * @param messageExt
     */
    @Override
    public void onMessage(MessageExt messageExt) {
        try {
            byte[] body = messageExt.getBody();
            String s = new String(body, Constants.UTF8);
            JSONObject jsonObject = JSONObject.parseObject(s);
            String orderSn = jsonObject.get("orderSn").toString();
            CourseOrder courseOrder = new CourseOrder();
            courseOrder.setStatusOrder(RConst.Num.INT_1);
            courseOrderMapper.update(courseOrder,new LambdaQueryWrapper<CourseOrder>().eq(CourseOrder::getOrderNo,orderSn));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
}
